package com.jwsoft.manager.core.integration.bigScreen.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bifang.engine.assistant.core.annotation.ApiService;
import com.bifang.module.base.common.vo.common.CommonVO;
import com.bifang.module.base.core.integration.CommonIntegration;
import com.jwsoft.manager.common.vo.bigScreen.analysisYj.*;
import com.jwsoft.manager.common.vo.eduSjtj04.EduSjtj04VO;
import com.jwsoft.manager.core.dao.model.EduSjtj02;
import com.jwsoft.manager.core.dao.model.EduSjtj04;
import com.jwsoft.manager.core.dao.model.EduStudentEnrollPre;
import com.jwsoft.manager.core.dao.service.EduSjtj02Service;
import com.jwsoft.manager.core.dao.service.EduSjtj04Service;
import com.jwsoft.manager.core.dao.service.EduStudentEnrollPreService;
import com.jwsoft.manager.core.integration.EduSjtj04Integration;
import com.jwsoft.manager.core.integration.bigScreen.AnalysisYjIntegration;
import com.jwsoft.manager.core.util.EduHelper;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@ApiService(funcCode = "AnalysisYj", title = "大屏接口")
public class AnalysisYjIntegrationImpl implements AnalysisYjIntegration {
    @Autowired
    private EduSjtj02Service eduSjtj02Service;
    @Autowired
    EduHelper eduHelper;
    @Autowired
    CommonIntegration commonIntegration;
    @Autowired
    EduStudentEnrollPreService eduStudentEnrollPreService;
    @Autowired
    EduSjtj04Service eduSjtj04Service;
    @Override
    public List<DegreeInfoVO> getDegreeInfo(DegreeInfoQueryVO vo) {
        if(ObjectUtils.isEmpty(vo.getYear())){
            vo.setYear(eduHelper.thisYear());
        }
        if(ObjectUtils.isEmpty(vo.getAreaCode())){
            CommonVO commonVO=new CommonVO();
            commonVO.setParamKey("area_code");
            String areaCode=commonIntegration.getValueByKey(commonVO);
            vo.setAreaCode(areaCode);
        }
        List<DegreeInfoVO> degreeInfoVOS=eduSjtj02Service.getDegreeInfoVO(vo);
        Integer[] arr={1};
        degreeInfoVOS=degreeInfoVOS.stream().peek(e->e.setNum(arr[0]++)).collect(Collectors.toList());

        return degreeInfoVOS.subList(0,Math.min(degreeInfoVOS.size(),20));
    }

    @Override
    public AnalysisDegreeVO getAnalysisDegreeInfo(DegreeInfoQueryVO vo) {
        if(ObjectUtils.isEmpty(vo.getYear())){
            vo.setYear(eduHelper.thisYear());
        }
        if(ObjectUtils.isEmpty(vo.getAreaCode())){
            CommonVO commonVO=new CommonVO();
            commonVO.setParamKey("area_code");
            String areaCode=commonIntegration.getValueByKey(commonVO);
            vo.setAreaCode(areaCode);
        }
        QueryWrapper<EduSjtj02> redWrapper=new QueryWrapper();
        redWrapper.eq("year",vo.getYear()).eq("area_code",vo.getAreaCode());
        List<EduSjtj02> eduSjtj02s=eduSjtj02Service.list(redWrapper);
        List<EduSjtj02> red=new ArrayList<>();
        List<EduSjtj02> yellow=new ArrayList<>();
        List<EduSjtj02> green=new ArrayList<>();
        if(eduSjtj02s.size()>0){
            eduSjtj02s.forEach(item->{
                if(item.getWy().compareTo(item.getEnrolment())>=0){
                    red.add(item);
                }
                if(item.getEnrolment().multiply(new BigDecimal(0.8)).compareTo(item.getWy())<=0){
                    yellow.add(item);
                }
                if(item.getEnrolment().multiply(new BigDecimal(0.8)).compareTo(item.getWy())<0){
                    green.add(item);
                }
            });
        }
        AnalysisDegreeVO analysisDegreeVO=new AnalysisDegreeVO();
        analysisDegreeVO.setGreen(green.size());
        analysisDegreeVO.setRed(red.size());
        analysisDegreeVO.setYellow(yellow.size());
        List<DegreeTopVO> degreeTopVOS=new ArrayList<>();
        List<DegreeTopVO> finalDegreeTopVOS = new ArrayList<>();
        if(red.size()>0){

            red.forEach(item->{
                    DegreeTopVO degreeTopVO=new DegreeTopVO();
                    degreeTopVO.setSchoolId(item.getSchoolId());
                    degreeTopVO.setSchoolName(item.getSchoolName());
                    degreeTopVO.setCount(item.getWy().subtract(item.getEnrolment()).intValue());
                finalDegreeTopVOS.add(degreeTopVO);
            });
        }
        Integer[] arr={1};
        degreeTopVOS=finalDegreeTopVOS.stream().sorted(Comparator.comparing(DegreeTopVO::getCount).reversed()).peek(s->s.setNum(arr[0]++)).collect(Collectors.toList());
        analysisDegreeVO.setDegreeList(degreeTopVOS.subList(0,Math.min(degreeTopVOS.size(),10)));
        //5年红色预警
        Map<String,Object> redParam =new HashMap<>();
        redParam.put("year",vo.getYear());
        redParam.put("areaCode",vo.getAreaCode());
        redParam.put("flag","WY > enrolment");
        List<Map<String,Object>> redResult=eduSjtj02Service.getFiveYearInfo(redParam);
        //5年黄色预警
        Map<String,Object> yellowParam =new HashMap<>();
        yellowParam.put("year",vo.getYear());
        yellowParam.put("areaCode",vo.getAreaCode());
        yellowParam.put("flag","WY >= (enrolment*0.8)");
        List<Map<String,Object>> yellowResult=eduSjtj02Service.getFiveYearInfo(yellowParam);
        //5年绿色预警
        Map<String,Object> greenParam =new HashMap<>();
        greenParam.put("year",vo.getYear());
        greenParam.put("areaCode",vo.getAreaCode());
        greenParam.put("flag","WY < (enrolment*0.8)");
        List<Map<String,Object>> greenResult=eduSjtj02Service.getFiveYearInfo(greenParam);
        Map<String,Object> fiveYearList=new HashMap<>();
        fiveYearList.put("redList",redResult);
        fiveYearList.put("greenList",greenResult);
        fiveYearList.put("yellowList",yellowResult);
        analysisDegreeVO.setFiveYearList(fiveYearList);
        return analysisDegreeVO;
    }

    @Override
    public DegreeSchDetailVO getDegreeDetail(DegreeDetailVO vo) {
        if(ObjectUtils.isEmpty(vo.getYear())){
            vo.setYear(eduHelper.thisYear());
        }
        if(ObjectUtils.isEmpty(vo.getAreaCode())){
            CommonVO commonVO=new CommonVO();
            commonVO.setParamKey("area_code");
            String areaCode=commonIntegration.getValueByKey(commonVO);
            vo.setAreaCode(areaCode);
        }
        QueryWrapper eduSjtj02Wrapper=new QueryWrapper();
        eduSjtj02Wrapper.eq("year",vo.getYear());
        eduSjtj02Wrapper.eq("area_code",vo.getAreaCode());
        eduSjtj02Wrapper.eq("school_id",vo.getSchoolId());
        List<EduSjtj02> eduSjtj02s=eduSjtj02Service.list(eduSjtj02Wrapper);
        DegreeSchDetailVO degreeSchDetailVO=new DegreeSchDetailVO();
        if(eduSjtj02s.size()>0){
            eduSjtj02s.forEach(item->{
                degreeSchDetailVO.setA(item.getA());
                degreeSchDetailVO.setB(item.getB());
                degreeSchDetailVO.setC(item.getC());
                degreeSchDetailVO.setOther(item.getOther());
                degreeSchDetailVO.setSchoolId(item.getSchoolId());
                degreeSchDetailVO.setSchoolName(item.getSchoolName());
                degreeSchDetailVO.setEnrolment(item.getEnrolment());
                degreeSchDetailVO.setWY(item.getWy());
                degreeSchDetailVO.setVolunteerCount(eduStudentEnrollPreService.count(new QueryWrapper<EduStudentEnrollPre>().eq("school_id",item.getSchoolId())));
                if(item.getWy().compareTo(item.getEnrolment())>=0){
                    degreeSchDetailVO.setDegreeType("red");
                }
                if(item.getEnrolment().multiply(new BigDecimal(0.8)).compareTo(item.getWy())<=0){
                    degreeSchDetailVO.setDegreeType("yellow");
                }
                if(item.getEnrolment().multiply(new BigDecimal(0.8)).compareTo(item.getWy())<0){
                    degreeSchDetailVO.setDegreeType("green");
                }
            });
        }
                Map<String,Object> param=new HashMap<>();
                param.put("year",vo.getYear());
                param.put("schoolId",degreeSchDetailVO.getSchoolId());
                degreeSchDetailVO.setFiveYearList(eduSjtj02Service.getDegreeSchFiveYearInfo(param));
                List<EduSjtj04> eduSjtj04s=eduSjtj04Service.list(new QueryWrapper<EduSjtj04>().eq("school_id",degreeSchDetailVO.getSchoolId()));
                List<EduSjtj04VO> eduSjtj04VOS=new ArrayList<>();
                if(eduSjtj04s.size()>0){
                    eduSjtj04s.forEach(item->{
                        EduSjtj04VO eduSjtj04VO=new EduSjtj04VO();
                        eduSjtj04VO.setAddress(item.getAddress());
                        eduSjtj04VO.setCbTime(item.getCbTime());
                        eduSjtj04VO.setCbYs(item.getCbYs());
                        eduSjtj04VO.setSchoolId(item.getSchoolId());
                        eduSjtj04VO.setHouseholdId(item.getHouseholdId());
                        eduSjtj04VO.setHouseholdRegister(item.getHouseholdRegister());
                        eduSjtj04VO.setHouseholdTime(item.getHouseholdTime());
                        eduSjtj04VO.setHouseId(item.getHouseId());
                        eduSjtj04VO.setId(item.getId());
                        eduSjtj04VO.setNation(item.getNation());
                        eduSjtj04VO.setRegisterDate(item.getRegisterDate());
                        eduSjtj04VOS.add(eduSjtj04VO);
                    });
                }
                degreeSchDetailVO.setEduSjtj04List(eduSjtj04VOS);
        return degreeSchDetailVO;
    }
}
